package com.lan.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lan.SysConstants;
import com.lan.controller.BaseController;
import com.lan.entity.sys.Menu;
import com.lan.service.sys.MenuService;
import com.lan.utils.tree.TreeTableNode;
import com.lan.viewObject.Query;
import com.lan.viewObject.Result;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  菜单 Controller
 * </p>
 *
 * @author shadow
 * @since 2022-06-04
 */
@Controller
@RequestMapping("/menu")
@RequiresPermissions("menu:list")
public class MenuController extends BaseController {

    @Resource
    private MenuService menuService;

    /**
     * 列表 页.
     *
     * */
    @GetMapping("/list")
    public String list() {
        return "sys/menu/list";
    }

    /**
     * 新增 页.
     *
     * 在 id 对应菜单下新增
     * 首页下新增，约定为一级菜单
     *
     * */
    @GetMapping("/add/{id}")
    public String add(final ModelMap model, @PathVariable final String id) {
        model.addAttribute(SysConstants.MENU, new Menu());
        model.addAttribute("parentMenu", menuService.getById(id));
        return "sys/menu/add";
    }

    /**
     * 编辑 页.
     *
     * */
    @GetMapping("/edit/{id}")
    public String edit(final ModelMap model, @PathVariable("id") final String id) {
        Menu menu = menuService.getById(id);
        if (null == menu)
            menu = new Menu();
        model.addAttribute(SysConstants.MENU, menu);
        return "sys/menu/edit";
    }

    /**
     * 列表 -> 分页.
     *
     * */
    @ResponseBody
    @PostMapping("/list")
    public Result getList(@RequestParam final Map<String, Object> params) {
        Query<Menu> query = new Query<>(params);
            IPage<Menu> page = menuService.getPageByWrapper(query);
        return Result.ok()
                .put(Result.CODE, 0)
                .put(Result.STATUS, 200)
                .put(Result.DATA, page.getRecords())
                .put(Result.COUNT, page.getTotal())
                .put(Result.MSG, "");
    }

    @ResponseBody
    @PostMapping("/getTreeTableList")
    public Result getTreeTableList(@RequestParam final Map<String, Object> params) {
        Query<Menu> query = new Query<>(params);
        List<TreeTableNode> list = menuService.getTreeTableList(query);
        return Result.ok()
                .put(Result.CODE, 0)
                .put(Result.MSG, "...")
                .put(Result.COUNT, null)
                .put(Result.DATA, list);
    }

    /**
     * 保存.
     *
     * */
    @ResponseBody
    @PostMapping("/add")
    public Result add(@Valid final Menu menu) {
        if (menu.getParentId().equals(SysConstants.INDEX_ID)) {
            menu.setParentId(SysConstants.PARENT_ID).setParentName("");
        }
        if (1 <= menuService.save(menu)) {
            return Result.ok("保存成功");
        }
        return Result.error("保存失败");
    }

    /**
     * 更新.
     *
     * */
    @ResponseBody
    @PostMapping("/update")
    public Result update(final Menu menu) {
        if (StringUtils.isNotBlank(menu.getId())) {
            if (1 <= menuService.updateById(menu)) {
                return Result.ok("更新成功");
            }
            return Result.ok("更新失败");
        } else {
            return Result.error("缺少必要参数");
        }
    }

    /**
     * 删除.
     *
     * */
    @ResponseBody
    @PostMapping("/del/{id}")
    public Result del(@PathVariable("id") final String id) {
        if (1 <= menuService.deleteById(id)) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
